package com.kd.test;


import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.types.RedisClientInfo;

import java.util.*;
import java.util.concurrent.TimeUnit;

@SpringBootTest
public class Demo {


    @Autowired
    private RedisTemplate<String ,Object> redisTemplate = new RedisTemplate<>();


    /**
     * String类型的操作
     */
    @Test
    public void testSetString(){
        //设置键值对
        redisTemplate.opsForValue().set("k4","v4");
        //获取值
        Object k1 = redisTemplate.opsForValue().get("k1");
        System.out.println(k1);

        //删除
        Boolean flag = redisTemplate.delete("k1");
        System.out.println("是否删除成功"+flag);

        //判断键是否存在
        Boolean flag1 = redisTemplate.hasKey("k4");
        System.out.println("键是否存在"+flag1);

        Long k4 = redisTemplate.opsForValue().increment("4", 3);
        System.out.println(k4);

    }

    /**
     * 设置string类型过期时间
     */
    @Test
    public void testTimeOut(){
        //第一种
        redisTemplate.opsForValue().set("time","value",20,TimeUnit.SECONDS);
        //第二种
        redisTemplate.expire("time1",50,TimeUnit.SECONDS);

    }


    /**
     * 设置List类型的数据
     */
    @Test
    public void testList(){
        //设置键值对
        redisTemplate.opsForList().leftPush("keyList","valueList1");
        redisTemplate.opsForList().leftPush("keyList","valueList2");
        redisTemplate.opsForList().leftPush("keyList","valueList3");
        redisTemplate.opsForList().leftPush("keyList","valueList4");
        redisTemplate.opsForList().leftPush("keyList","valueList5");

        //获取值
        List<Object> list = redisTemplate.opsForList().range("keyList", 0, -1);
        for (Object obj :
                list) {
            System.out.println(obj);
        }

    }

    /**
     * 操作Set类型的数据
     */
    @Test
    public void testSet(){


        //设置键值对
        redisTemplate.opsForSet().add("keySet","valueSet1","valueSet2","valueSet3","valueSet4");

        //获取值
        Set<Object> set = redisTemplate.opsForSet().members("keySet");

        for (Object obj :
                set) {
            System.out.println(obj);
        }

        //是否存在
        Boolean flag = redisTemplate.opsForSet().isMember("keySet", "valueSet2");
        System.out.println(flag);

        //判断长度
        Long length = redisTemplate.opsForSet().size("keySet");
        System.out.println(length);

        //移除
        redisTemplate.opsForSet().remove("keySet","valueSet4");
    }


    /**
     * 操作Hash类型的数据
     */
    @Test
    public void testHsh(){

        //设置1
        redisTemplate.opsForHash().put("keyHash","keyValueHash1","valueValueHash1");
        //设置2
        Map<String,String> map = new HashMap<>();
        map.put("keyMap","keyValue");
        map.put("keyMap1","keyValue1");
        map.put("keyMap2","keyValue2");
        map.put("keyMap3","keyValue3");
        map.put("keyMap4","keyValue4");
        redisTemplate.opsForHash().putAll("keyHash",map);

        //获取
        Object value = redisTemplate.opsForHash().get("keyHash", "keyValueHash1");
        System.out.println(value);

        //获取Hash类型所有的键
        Set<Object> keys = redisTemplate.opsForHash().keys("keyHash");
        for (Object o :
                keys) {
            System.out.println(o);
        }

        //获取所有的键值对集合
        Map<Object, Object> entries = redisTemplate.opsForHash().entries("keyHash");
        Iterator<Map.Entry<Object, Object>> iterator = entries.entrySet().iterator();
        while (iterator.hasNext()){
            Map.Entry<Object, Object> entry = iterator.next();
            Object key = entry.getKey();
            Object value1 = entry.getValue();
            System.out.println(key + " :" + value);
        }

        //删除
        redisTemplate.opsForHash().delete("keyHash","keyMap");

        //判断是否有该键值
        Boolean flag = redisTemplate.opsForHash().hasKey("keyHash", "keyMap");
        System.out.println(flag);

    }

    /**
     * 操作Hash类型的过期时间
     */
    @Test
    public void testTimeOutHash(){

    }

    /**
     * 操作Zset类型
     */
    @Test
    public void testZset(){
        //设置值
        redisTemplate.opsForZSet().add("keyZset","valueZset4",50);
        redisTemplate.opsForZSet().add("keyZset","valueZset2",30);
        redisTemplate.opsForZSet().add("keyZset","valueZset5",60);
        redisTemplate.opsForZSet().add("keyZset","valueZset1",20);
        redisTemplate.opsForZSet().add("keyZset","valueZset3",40);


        //获取   根据分数获取
        Set<Object> zset = redisTemplate.opsForZSet().rangeByScore("keyZset",20,50);


        for (Object obj :
                zset) {
            System.out.println(obj);
        }
        System.out.println("______________________________________");

        //获取
        Set<Object> zset1 = redisTemplate.opsForZSet().range("keyZset", 0, -1);
        for (Object o:
                zset1) {
            System.out.println(o);
        }


        //获取元素的分数
        Double score = redisTemplate.opsForZSet().score("keyZset", "valueZset2");
        System.out.println(score);

        //返回集合的个数
        Long num = redisTemplate.opsForZSet().size("keyZset");
        System.out.println(num);

        //删除 指定值
        redisTemplate.opsForZSet().remove("keyZset","valueZset3");
    }

}
